global class ApexScheduledClassAttendance Implements Schedulable
{
public List<User> listUsers{get; set;}
public List<User> listUsers2{get; set;}
public List<Attendance_Record__c> listAttendance{get; set;}
public Integer count{get; set;}

public List<Personal_Time_Off__c> listTimeOff{get; set;}
public List<LoginHistory> loginToday{get; set;}
//public Boolean attendance{get; set;}
public Integer diference{get; set;}
public Datetime systemDate{get; set;}

        global void execute(SchedulableContext sc)
        {
            checkAttendance();
        }
        public void checkAttendance()
        {
        Datetime current = datetime.now();
        diference = current.hourgmt() - current.hour();
        systemDate = current.addHours(diference);
        listUsers=[SELECT Id, AccountId, Working_Hours__c, Attendance_Checked__c, Approved_Time_Off__c FROM User WHERE (AccountId != NULL) AND (Attendance_Checked__c = FALSE) AND (Approved_Time_Off__c = FALSE)];
        loginToday=[SELECT UserId, LoginTime, Status FROM LoginHistory WHERE LoginTime = TODAY];
        listTimeOff=[Select Name, Date__c, Number_Days__c, Employee__c, Approved__c, EndDate__c FROM Personal_Time_Off__c WHERE (Approved__c = TRUE) AND (EndDate__c >= TODAY)];
        List<Attendance_Record__c> attendanceList = new List<Attendance_Record__c>();
        for (User userTemp: listUsers)
        {
            for (LoginHistory log: loginToday)
            {
                if (userTemp.Id == log.UserId)
                {
                    if (log.Status=='success')
                    {
                        userTemp.Attendance_Checked__c = TRUE;
                    }
                }
            }
            for (Personal_Time_Off__c timeOff: listTimeOff){
                if (timeOff.Employee__c == userTemp.AccountId)
                {
                    userTemp.Approved_Time_Off__c = TRUE;
                }
            }    
        }
        update listUsers;
        listUsers2=[SELECT Id, Name, AccountId, Working_Hours__c, Attendance_Checked__c, Approved_Time_Off__c, Inattendance_On_7_Days__c FROM User WHERE (AccountId != NULL) AND (Attendance_Checked__c = FALSE) AND (Approved_Time_Off__c = FALSE)];
        for (User userTemp2: listUsers2)
        {
            attendanceList.add (new Attendance_Record__c(
             User__c = userTemp2.Id,
             Account_Id__c = userTemp2.AccountId,
             Employee_Name__c = userTemp2.Name,
             Date_of_inattendance__c = system.today()));
        }
        listUsers=[SELECT Id, Attendance_Checked__c, Approved_Time_Off__c FROM User WHERE (AccountId != NULL) AND ((Attendance_Checked__c = TRUE) OR (Approved_Time_Off__c = TRUE))];
        for (User userTemp3: listUsers)
        {
        userTemp3.Attendance_Checked__c = FALSE;
        userTemp3.Approved_Time_Off__c = FALSE;
        }
        insert attendanceList;
        update listUsers;
        listAttendance= [Select User__c, Date_of_inattendance__c, Account_Id__c FROM Attendance_Record__c WHERE Date_of_inattendance__c >= LAST_WEEK];
        for (User userTemp4: listUsers2)
        {
        count=1;
            for (Attendance_Record__c attendanceTemp: listAttendance)
            {
                if (attendanceTemp.Account_Id__c==userTemp4.AccountId)
                {
                count=count+1;
                }
        
            }
        userTemp4.Inattendance_On_7_Days__c=count;
        }
        update listUsers2;

            
        }
        
     //       global static void testSchedule()
     //   {
     //       Test.startTest();
     //       MyScheduledProcess sched = new MyScheduledProcess();
     //       Id job_id = System.schedule('test', '0 0 0 30 12 ? 2099', sched);
     //       System.assertNotEquals(null, job_id);
     //       // TODO: test your code
     //       Test.stopTest();
     //   } 
}